package com.air.ota.vo;

import com.air.ota.util.ConfigProperties;
import com.air.ota.util.HexStringUtil;
import com.air.ota.util.HsmjUtil;
import com.air.ota.util.PBOCUtil;


/**
 *

 * @author Marco
 *
 */
public class UTKBaseUplinkPgkVO {
	
	
	//protected byte   udl;
	
	protected byte   udhl;
	protected byte   ieia;
	protected byte   ieidla;
	protected byte[] cpl;
	protected byte   chl;
	protected byte[] spi;
	protected byte   kic;
	protected byte   kid;
	protected byte[] tar;
	protected byte[] cntr;
	protected byte   pcntr;
	
	
	protected byte   cmdType;
	protected byte   cmdVer;
	protected byte[] cmdLength;
	protected byte[] cmdData;
	protected byte[] encCmdData;
	
	protected byte[] random;
	protected byte[] mac;
	
	protected byte[] UTKSessionKey; 
	
	protected byte[] cardSequence;
	
	public UTKBaseUplinkPgkVO(){
		this.udhl =0x02;
		this.ieia = 0x70;
		this.ieidla = 0x00;
		this.cpl = new byte[2];
		this.chl = (byte)0x0D;
		this.spi = HexStringUtil.hexStringToBytes("0000");
		this.kic = 0x00;
		this.kid = 0x00;
		this.tar = HexStringUtil.hexStringToBytes("09008F");
		this.cntr = HexStringUtil.hexStringToBytes("0000000000");
		this.pcntr = 0x00;
		
		
		
		this.random =new byte[4];
		this.mac =new byte[4];
		
		this.cmdLength = new byte[2];
		this.cardSequence = new byte[8];
	}
	
	public void parse(byte[] pkg){
		
		int randomStartPos = pkg.length-8;
		int macStartPos = pkg.length-4;
		System.arraycopy(pkg, randomStartPos, this.random, 0, 4);
		System.arraycopy(pkg, macStartPos, this.mac, 0, 4);
		
		
		
		System.arraycopy(pkg, 3, this.cpl, 0, 2);
		this.cmdType = pkg[19];
		this.cmdVer = pkg[20];
		System.arraycopy(pkg, 21, cmdLength, 0, 2);
		
		//开通请求部分需要重写因为，要去1byte卡商代码 去卡片序列号8byte
		int encCmdDataLength = pkg.length-23-8;//去头23byte，去mac和random8byte
		encCmdData = new byte[encCmdDataLength];
		System.arraycopy(pkg, 23, encCmdData, 0, encCmdDataLength);
	}

	public byte getCmdType() {
		return cmdType;
	}
	
	public void decryptCmdData(){
		PBOCUtil pbocUtil = new PBOCUtil();
		UTKSessionKey = pbocUtil.buildUTKSessionKey(cardSequence, this.random);
		
		//使用本地软件解密
		//String tmpCmdDataStr = HexStringUtil.bytesToHexString(pbocUtil.TripleDes(UTKSessionKey, encCmdData, 0));
		
		//使用加密机解密
		String tmpCmdDataStr = HexStringUtil.bytesToHexString(HsmjUtil.decrypt(cardSequence, random, encCmdData));
		
		System.out.println("cardSequence="+HexStringUtil.bytesToHexString(cardSequence));
		System.out.println("random="+HexStringUtil.bytesToHexString(random));
		System.out.println("Decrypted Data="+tmpCmdDataStr);
		
		try{
			this.cmdData = HexStringUtil.hexStringToBytes(tmpCmdDataStr.substring(0,tmpCmdDataStr.lastIndexOf("80")));
		}catch(Exception ex){
			ex.printStackTrace();
			System.out.println("----------------null Exception-----------------------");
			System.out.println("Decrypted Data use HSMJ:  "+tmpCmdDataStr);
			System.out.println("----------------null Exception-----------------------");
		}
		
		System.out.println("UTK Uplink cmddata(decrypted)="+HexStringUtil.bytesToHexString(cmdData));
		
		
	}

	public byte[] getCardSequence() {
		return cardSequence;
	}
	
	
	
	
}
